#ifndef _TRANSMITER_FILE_H_
#define	_TRANSMITER_FILE_H_

#include "stdint.h"
#include "platform.h"

#define __PACK__     __attribute__((packed))

/// Input sources are
/// CONST_0/CONST_1
/// POT_1 ~ POT(NUM_POTS)/-POT1 ~ -POT(NUM_POTS)
/// SW1 ~SW(NUM_SW)/!SW1 ~ !SW(NUM_SW)
/// VSW1 ~ VSW(NUM_VSW)/!VSW1 ~ !VSW(NUM_VSW)
/// VPOT1 ~ VPOT(NUM_VPOT)/-VPOT1 ~ -VPOT(NUM_VPOT)
/// 

/// [POT][SW][VSW][VPOT]
#if 0
#define SRC_BASE_POT (0)
#define CH_POT(x) (x-SRC_BASE_POT)
#define SRC_BASE_SW  (NUM_POTS)
#define CH_SW(x) (x-SRC_BASE_SW)
#define SRC_BASE_VSW (SRC_BASE_SW +NUM_SW_POSITIONS)
#define CH_VSW(x) (x-SRC_BASE_VSW)
#define SRC_BASE_CONST (SRC_BASE_VSW)
#define SRC_BASE_VPOT (SRC_BASE_VSW+NUM_VSW)
#define CH_VPOT(x) (x-SRC_BASE_VPOT)
#define SRC_BASE_MAX (SRC_BASE_VPOT +NUM_VPOT)
#else
#define CH_POT(x) (x)
#define CH_SW(x) (x)
#define CH_VSW(x) (x)
#define CH_VPOT(x) (x)
#endif

/// trainer port setting for 1 trainer channel
/// calibration setting for 1 potentialmeter
typedef struct t_Calibration 
{
    int16_t mid;    ///< center value: range(2048+-400)
    int16_t up;      ///< up value: range 3072~4095
    int16_t low;    ///< low value: range 0~1024
    uint8_t filter:4;    ///< 0-12: 0:no filter; 12 most smooth
    uint8_t invert:1; ///< input revert
} __PACK__ CaliData;


/// Virtual Switches
#define BOOLEAN_NOT  (0)
#define BOOLEAN_OR (1)
#define BOOLEAN_AND (2)
/// Virtual source types
#define SRC_TYPE_POT (0)
#define SRC_TYPE_SW (1)
#define SRC_TYPE_VPOT (2)
#define SRC_TYPE_VSW (3)
//#define SRC_TYPE_CONST  (4)
/// virtual pot
#define CURVE_TYPE_LINE (0)
#define CURVE_TYPE_SMOOTH (1)

typedef struct{
    uint8_t id:6;      ///< source number
    uint8_t type:2;
}Source;
/// virtual channel setting for 1 virtual channel
typedef struct t_VirtualChannel {
    Source source;      ///< source
    union {
        struct {
            int16_t value;      ///< value for true;
            Source ch2; ///< when false, value depends on ch2; to create multi switch values
        }bool_pot;
        struct {
            uint8_t boolean;	///< 2: "OR"; 1: "and"; 0: "NOT"
            Source ch2;     ///< if combined to another channel
        }bool_bool;
        struct{
            float point[7];   ///< 7-points curve, if source is pot;
            int8_t type;
        } pot_pot;
        struct{
            uint16_t value;    ///< TRUE if(x>value) 
        }pot_bool;
    }alg;         ///< algorithm to get this channel
    char name[LEN_VCH_NAME];
} __PACK__ VChannel;

/// transmitter file
typedef struct t_TransmitterFile {
    uint8_t   version;          ///< current version
    uint16_t  variant;          ///<
    uint16_t  chkSum;           ///< sumed up should be 0
    char      ownerName[LEN_USER_NAME];        ///< owner name
    uint32_t  hours;            ///< hours since born
    uint8_t   minutes;          ///< minutes since born
    CaliData  cali[NUM_POTS];   ///< inputs calibration
    int8_t    currModel;        ///< current model ID
    uint8_t   vBatWarn;         ///< battery warning value
    int8_t    vBatCalib;        ///< battery calibration value, should be calculated to vBat
    uint8_t   contrast;         ///< LCD contrast
    int8_t    backlightMode;    ///< back light mode: 0:off;1:backlight for key; 2: backlight for stick;
    int8_t    backlightValue;   ///< back light pwm value: 1-255;
    VChannel  vSW[NUM_VSW];     ///< virtual SW
    VChannel  vPot[NUM_VPOT];   ///< virutal pots 
    int8_t    buzzerMode:2;     ///< -2=quiet, -1=only alarms, 0=no keys, 1=all
    int8_t    beepMode:2;       ///< -2=quiet, -1=only alarms, 0=no keys, 1=all
    uint8_t   disableMemoryWarning:1;    ///< no warning for low memory
    uint8_t   disableAlarmWarning:1;     ///< no warning for alarm
    uint8_t   stickMode:2;      ///< RETA mode
    uint8_t   timezone;         ///< country code
    uint8_t   inactivityTimer;  ///< inactivity timer value, in seconds
    uint8_t   lightAutoOff;     ///< time last for back light off
    uint8_t   templateSetup;    ///< channel outputs map
    uint8_t   unexpectedShutdown:1;     ///< check for power failure
    int8_t    speakerVolume;    ///< speaker volume
    int8_t    beepVolume;
    int8_t    waveVolume;
    int8_t    vBatMin;          ///< battery min

}__PACK__ TransmiterFile;




#endif



